package net.beyondts.example.mplug.controller;

import com.alibaba.fastjson.JSON;
import net.beyondts.example.mplug.extension.SomeService;
import net.beyondts.mplug.test.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.Map;

/**
 * Project: mplug-project<br/>
 * Description: <br/>
 * Created at: 2019-1-12<br/>
 * Owned by beyondts.
 *
 * @author blazecrystal
 * @version 1.0.0
 */
@RestController
@RequestMapping("/some")
public class SomeController {
    @RequestMapping("/test")
    public String test(HttpServletRequest req) {
        return "this is a test controller";
    }

    @Autowired
    private SomeService service;

    @Autowired
    private Test test;

    private JdbcTemplate template;

    @Autowired
    public SomeController(JdbcTemplate template) {
        this.template = template;
    }

    @RequestMapping(value = "/exec/{sql}", method = RequestMethod.GET)
    public String sqlExec(@PathVariable String sql) {
        Map<String, Object> rst = template.queryForMap(sql);
        return JSON.toJSONString(rst);
    }


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC", "root", "beyondts");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from acct");
            while(rs.next()) {
                System.out.println(rs.getString("NAME"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        service.print();
        service.print(100);
        service.print(10);
        service.print("this is auto gen extension", 12);
        service.testPrint("------------");
//        System.out.println(service.test("--- this is in "));
        test.testout();

        new Test().testout();
        return "Hello Mplug";
    }
}
